﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DtCms.Common;

namespace DtCms.Web.Admin.Pictures
{
    public partial class Edit : DtCms.Web.UI.ManagePage
    {
        public int Id;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!int.TryParse(Request.Params["id"] as string, out this.Id))
            {
                JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                return;
            }
            if (!Page.IsPostBack)
            {
                chkLoginLevel("editProducts");
                //绑定类别
                ChannelTreeBind(0, "请选择所属类别...", (int)Channel.Pictures, this.ddlClassId);
                FieldBind();
                ShowInfo(this.Id);
            }
        }

        //绑定扩展字段
        private void FieldBind()
        {
            DtCms.BLL.PicturesField bll = new DtCms.BLL.PicturesField();
            rptField.DataSource = bll.GetList("");
            rptField.DataBind();
        }

        //输出输出扩展字段的CSS
        protected string WriteCss(bool isNull, string fieldType)
        {
            StringBuilder str = new StringBuilder();
            str.Append("input");
            if (isNull == false)
                str.Append(" required");
            if (fieldType.Trim() == "Numeric")
                str.Append(" number");
            return str.ToString();
        }

        //赋值操作
        private void ShowInfo(int _id)
        {
            DtCms.BLL.Pictures bll = new DtCms.BLL.Pictures();
            DtCms.Model.Pictures model = bll.GetModel(_id);

            txtTitle.Text = model.Title;
            txtPrice.Text = model.Price.ToString();
            ddlClassId.SelectedValue = model.ClassId.ToString();
            txtContent.Value = model.Content;
            txtClick.Text = model.Click.ToString();
            txtSortId.Text = model.SortId.ToString();
            if (model.IsMsg == 1)
            {
                cblItem.Items[0].Selected = true;
            }
            if (model.IsTop == 1)
            {
                cblItem.Items[1].Selected = true;
            }
            if (model.IsRed == 1)
            {
                cblItem.Items[2].Selected = true;
            }
            if (model.IsHot == 1)
            {
                cblItem.Items[3].Selected = true;
            }
            if (model.IsSlide == 1)
            {
                cblItem.Items[4].Selected = true;
            }
            if (model.IsLock == 1)
            {
                cblItem.Items[5].Selected = true;
            }
            //赋值上传的相册
            rptAlbums.DataSource = model.PicturesAlbums;
            rptAlbums.DataBind();
            //赋值扩展字段
            if (model.PicturesExtensions != null)
            {
                for (int i = 0; i < this.rptField.Items.Count; i++)
                {
                    int hideId = Convert.ToInt32(((HiddenField)this.rptField.Items[i].FindControl("hideFieldId")).Value);
                    foreach (DtCms.Model.PicturesExtension emodel in model.PicturesExtensions)
                    {
                        if (hideId == emodel.FieldId)
                        {
                            ((HiddenField)this.rptField.Items[i].FindControl("hideExtensionId")).Value = emodel.Id.ToString();
                            ((TextBox)this.rptField.Items[i].FindControl("txtFieldContent")).Text = emodel.Content;
                        }
                    }
                }
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            DtCms.BLL.Pictures bll = new DtCms.BLL.Pictures();
            DtCms.Model.Pictures model = bll.GetModel(this.Id);

            model.Title = txtTitle.Text.Trim();
            model.Price = decimal.Parse(txtPrice.Text.Trim());
            model.ClassId = int.Parse(ddlClassId.SelectedValue);
            //检查图片是否有变
            string hideFiles = Request.Params["hideFiles"];
            if (!string.IsNullOrEmpty(hideFiles))
            {
                string[] fileArr = hideFiles.Split(',');
                
                if (model.ImgUrl.Substring(model.ImgUrl.LastIndexOf("/") + 1) != fileArr[0].Substring(fileArr[0].LastIndexOf("/") + 1))
                {
                    //删除旧的缩略图
                    DeleteFile(model.ImgUrl);
                    //生成缩略图
                    model.ImgUrl = MakeThumbnail(fileArr[0]);
                }
            }
            else
            {
                //删除旧的缩略图
                DeleteFile(model.ImgUrl);
                //没有图片
                model.ImgUrl = "";
            }
            model.Content = txtContent.Value;
            model.Click = int.Parse(txtClick.Text.Trim());
            model.SortId = int.Parse(txtSortId.Text.Trim());
            model.IsMsg = 0;
            model.IsTop = 0;
            model.IsRed = 0;
            model.IsHot = 0;
            model.IsSlide = 0;
            model.IsLock = 0;
            if (cblItem.Items[0].Selected == true)
            {
                model.IsMsg = 1;
            }
            if (cblItem.Items[1].Selected == true)
            {
                model.IsTop = 1;
            }
            if (cblItem.Items[2].Selected == true)
            {
                model.IsRed = 1;
            }
            if (cblItem.Items[3].Selected == true)
            {
                model.IsHot = 1;
            }
            if (cblItem.Items[4].Selected == true)
            {
                model.IsSlide = 1;
            }
            if (cblItem.Items[5].Selected == true)
            {
                model.IsLock = 1;
            }
            //扩展字段
            List<DtCms.Model.PicturesExtension> els = new List<DtCms.Model.PicturesExtension>();
            for (int i = 0; i < rptField.Items.Count; i++)
            {
                int _id = 0;
                if (!string.IsNullOrEmpty(((HiddenField)rptField.Items[i].FindControl("hideExtensionId")).Value))
                {
                    _id = Convert.ToInt32(((HiddenField)rptField.Items[i].FindControl("hideExtensionId")).Value);
                }
                int _fieldid = Convert.ToInt32(((HiddenField)rptField.Items[i].FindControl("hideFieldId")).Value);
                string _title = ((HiddenField)rptField.Items[i].FindControl("hideFieldTitle")).Value;
                string _content = ((TextBox)rptField.Items[i].FindControl("txtFieldContent")).Text;
                els.Add(new DtCms.Model.PicturesExtension { Id = _id, PictureId = this.Id, FieldId = _fieldid, FieldName = _title, Content = _content });
            }
            model.PicturesExtensions = els;

            bll.Update(model);
            //保存日志
            SaveLogs("[图文模块]编辑图文：" + model.Title);
            JscriptPrint("图文编辑成功啦！", "List.aspx", "Success");
        }
    }
}
